package com.dys.datastructuresandalgorithm.presum;

/**
 * @className: com.dys.datastructuresandalgorithm.presum.PreSum
 * @description: 前缀和
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/05/30 下午11:16
 */
public class PreSum {

    private int[] preSumArray;

    public PreSum(int[] array) {
        int N = array.length;
        preSumArray = new int[N];
        preSumArray[0] = array[0];
        for (int i = 1; i < N; i++) {
            preSumArray[i] = preSumArray[i - 1] + array[i];
        }
    }

    public int rangeSum(int L, int R) {
        return L == 0 ? preSumArray[R] : preSumArray[R] - preSumArray[L - 1];
    }
}
